<?php
/* $a =new message ();
$a->SendMessage("oiZ5xs_VD7btMZIL_txNhu35JJ9w","您得订单已在路上，点击查看订单详情 wx.dev.renrencaichang.com/test_userinfo"); */

class SendMsgToUser {
	public  function SendMessage($openid,$mess){ 
		 define("APPID", "wxebfe8cfda2f5800e");
		 define("APPSECRET", "b292ceb6a0a6241fd1bb3d2d9ae885fd");
		 $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID  . "&secret=" . APPSECRET; 
		 $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
		 $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
		 $access_token = $result['access_token'];//得到token
		 $post = array("touser" => $openid,
					  "msgtype" => "text",
					  "text" => array("content"=>$mess)
		);
		$json = json_encode($post);
		$json_date=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $json); //将unicode转成utf-8
		$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token";
		$result =$this->https_request($url,$json_date );
		return $result;
	}
	public  function https_request($url,$data = null){
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		if (!empty($data)){
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}
}